package com.xmb.wechat.viewholder;

import android.app.Activity;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.Group;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.bean.PYQBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.widget.rc.RCConstraintLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/13 09:45
 */
public class PYQHeaderViewHolder {

    private WechatContactBean wechatContactBean;
    public  View              headerView;

    @BindView(R2.id.iv_cover)
    ImageView          mIvCover;
    @BindView(R2.id.tv_change_cover)
    TextView           mTvChangeCover;
    @BindView(R2.id.rc_owner)
    RCConstraintLayout mRcOwner;
    @BindView(R2.id.iv_owner)
    ImageView          mIvOwner;
    @BindView(R2.id.tv_owner)
    TextView           mTvOwner;
    @BindView(R2.id.iv_unread)
    ImageView          mIvUnread;
    @BindView(R2.id.tv_unread)
    TextView           mTvUnread;
    @BindView(R2.id.gp_unread)
    Group              mGpUnread;

    private Context context;

    public PYQHeaderViewHolder(Activity activity, PYQBean pyqBean, WechatContactBean wechatContactBean) {
        this.context = activity;
        this.wechatContactBean = wechatContactBean;
        headerView = LayoutInflater.from(context).inflate(R.layout.head_pyq, null, false);
        ButterKnife.bind(this, headerView);
        init(pyqBean);
    }

    private void init(PYQBean pyqBean) {
        if (pyqBean == null)
            return;
        //背景
        if (TextUtils.isEmpty(pyqBean.getCover())) {
            mTvChangeCover.setVisibility(View.VISIBLE);
            Picasso.get().load(R.mipmap.friendactivity_mycover_bg).into(mIvCover);
        } else {
            mTvChangeCover.setVisibility(View.GONE);
            Picasso.get().load("file:" + pyqBean.getCover()).into(mIvCover);
        }
        //朋友圈主人
        long ownerId = pyqBean.getOwnerId();
        WechatContactBean owner;
        if (ownerId == -1) {
            owner = wechatContactBean;
        } else {
            owner = WechatContactBean.getWechatContactBean(context, pyqBean.getOwnerId(), false);
        }
        if (owner != null) {
            mTvOwner.setText(TextUtils.isEmpty(owner.getName()) ? "" : owner.getName());
            WechatContactBean.setupAvatarIntoImageView(owner, mIvOwner);
        }
        //未读消息
        if (pyqBean.isShowUnreadMsg()) {
            //未读消息
            WechatContactBean unReader = WechatContactBean.getWechatContactBean(context, pyqBean.getUnReaderId(), false);
            if (unReader != null) {
                WechatContactBean.setupAvatarIntoImageView(unReader, mIvUnread);
                mTvUnread.setText(TextUtils.isEmpty(pyqBean.getUnReadNum()) ? "" : pyqBean.getUnReadNum() + "条新消息");
            }
            mGpUnread.setVisibility(View.VISIBLE);
            //控件高度
            headerView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (ScreenUtils.getScreenHeight() * 0.558)));
        } else {
            mGpUnread.setVisibility(View.GONE);
            //控件高度
            headerView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (ScreenUtils.getScreenHeight() * 0.454)));
            //封面高度
            ViewGroup.LayoutParams layoutParams = mIvCover.getLayoutParams();
            layoutParams.height = (int) (ScreenUtils.getScreenHeight() * 0.395);
            mIvCover.setLayoutParams(layoutParams);
            //头像高度
            ConstraintLayout.LayoutParams layoutParamsAvatar = (ConstraintLayout.LayoutParams) mRcOwner.getLayoutParams();
            layoutParamsAvatar.verticalBias = 0.9f;
            mRcOwner.setLayoutParams(layoutParamsAvatar);
        }
    }
}
